package com.qf.dao.impl;

import com.qf.dao.UserDao;
import com.qf.entity.User;
import com.qf.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * @author 10202
 * @version V1.0
 * @Project xiaomi
 * @Package com.qf.dao.impl
 * @Date 2023/4/13 14:54
 */
public class UserDaoImpl implements UserDao {
    @Override
    public void add(User user) {
        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());
        Object[] params={user.getId(),user.getUsername(),user.getPassword(),user.getEmail(),user.getGender(),user.getFlag(),user.getRole(),user.getCode()};
        try {
            qr.update("insert into tb_user values (?,?,?,?,?,?,?,?)",params);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }

    }

    @Override
    public User selectByUsername(String username) {
        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());
        try {
            return qr.query("select * from tb_user where username=?",new BeanHandler<>(User.class),username);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }

    @Override
    public User login(String username, String password) {
        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());
        try {
            return qr.query("select * from tb_user where username=? and password=?",new BeanHandler<>(User.class),username,password);
        }catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }

    }
}
